# -*- coding: utf-8 -*-
 
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def test_fact_create(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)

    def test_fact_create_repeat_code(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/create', p)
        self.assertNotEqual(res['code'], 0)

    def test_fact_delete(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/list')
        self.assertEqual(res['code'], 0)
        count = res['data']['count']

        res = self.c.open('/analytics/fact/delete', {'code': p['code']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], count - 1)

    def test_fact_list(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/list')
        self.assertEqual(res['code'], 0)
        count = res['data']['count']

        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/list')
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], count + 1)

    def test_fact_update(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['code'], p['code'])

        id = res['data']['id']

        p = {
            'id': id,
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/update', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/read', {'id': id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['code'], p['code'])
        self.assertEqual(res['data']['name'], p['name'])
        self.assertEqual(res['data']['table'], p['table'])

    def test_fact_update_part(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['code'], p['code'])

        id = res['data']['id']

        p2 = {
            'id': id,
            'code': self.randchar(12),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/update', p2)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact/read', {'id': id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['code'], p2['code'])
        self.assertEqual(res['data']['name'], p['name'])
        self.assertEqual(res['data']['table'], p2['table'])

    def test_fact_update_repeat_code(self):
        p1 = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p1)
        self.assertEqual(res['code'], 0)

        p2 = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p2)
        self.assertEqual(res['code'], 0)

        p2_id = res['data']['id']

        p = {
            'id': p2_id,
            'code': p1['code'],
        }
        res = self.c.open('/analytics/fact/update', p)
        self.assertNotEqual(res['code'], 0)


test_list = [
    'test_fact_create',
    'test_fact_create_repeat_code',
    'test_fact_delete',
    'test_fact_list',
    'test_fact_update',
    'test_fact_update_part',
    'test_fact_update_repeat_code',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



